﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Logic;
using TrainingsApp.Model;
using TrainingsApp.Helper;

namespace TrainingsApp.IPADApp.Permission
{
    public partial class RoleEdit : PageBase
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                QueryStringManager mgr = new QueryStringManager(this.Request);

                if (mgr.ActionCode == ClientConstSet.Update || mgr.ActionCode == ClientConstSet.Select)
                {
                    if (!string.IsNullOrEmpty(mgr.BizCode))
                    {
                        T_ROLE_Entity entity = Sys_Permission_Logic.T_ROLE_View(Convert.ToDecimal(mgr.BizCode));
                        if (entity != null)
                        {
                            bool readOnly = mgr.ActionCode == ClientConstSet.Select;
                            btnSave.Visible = !readOnly;
                            FormBinding.BindObjectToControls(entity, form1, !readOnly);
                        }
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            T_ROLE_Entity entity = new T_ROLE_Entity();
            FormBinding.BindControlsToObject(entity, form1);
            QueryStringManager mgr = new QueryStringManager(this.Request);
            if (mgr.ActionCode == ClientConstSet.Add)
            {
                entity.CREATEDATE = DateTime.Now;
                AddRole(entity);
            }
            else if (mgr.ActionCode == ClientConstSet.Update)
            {
                entity.ID = Convert.ToInt32(mgr.BizCode);
                UpdateRole(entity);
            }
            MessageBox.Invoke("parent.Refresh('{0}')", "btnSelect");
        }

        private void AddRole(T_ROLE_Entity entity)
        {
            Sys_Permission_Logic.T_ROLE_Add(entity);
        }

        private void UpdateRole(T_ROLE_Entity entity)
        {
            Sys_Permission_Logic.T_ROLE_Update(entity);
        }
    }
}